<?php

require_once( 'flexical/model/query.php' );

require_once( 'flexical/admin/view.php' );
require_once( 'flexical/admin/query-response.php' );

abstract class Flexical_Admin_Basic_Query_View extends Flexical_Admin_View
{
	public function render_query_form( $query, $request_type, $submit_button_text, $cancel_enabled, $mode )
	{
		$info = new Flexical_Admin_Form_Table_Info();

		$info->request_type = $request_type;
		$info->submit_button_text = $submit_button_text;
		$info->cancel_enabled = $cancel_enabled;

		if ( $query == null )
		{
			$query = new Flexical_Model_Query();
		}

		else
		{
			$info->rows[ 'ID' ] = $query->id;
			$info->hiddens[ 'query_id' ] = $query->id;
		}

		if ( $mode == 'readonly' )
		{
			$dates = array( 'relative' => 'Relative', 'absolute' => 'Absolute', 'default' => 'Default' );

			$info->rows[ 'Title' ] = esc_html( $query->title );
			$info->rows[ 'Description' ] = $this->build_query_description( $query );
		}

		else if ( $mode == 'readwrite' )
		{
			$info->rows[ 'Title' ] = "<input type=\"text\" name=\"query_title\" size=\"50\" value=\"" . esc_html( $query->title ) . "\" />\n";

			$info->rows[ 'Only show all future events?' ] =
				$this->get_radio_button_input( 'query_futureevents', array( 'true' => 'Yes', 'false' => 'No' ), $query->futureevents ? 'true' : 'false',
				"Note: Setting this value to 'Yes' will override the recurrence expansion start/end and min/max start values" );

			$info->rows[ 'Expand recurring events into single events?' ] = 
				$this->get_radio_button_input( 'query_singleevents', array( 'true' => 'Yes', 'false' => 'No' ), $query->singleevents ? 'true' : 'false' );

			$info->rows[ 'Recurrence exp. start date' ] = "<input type=\"text\" name=\"query_recurrence_expansion_start\" size=\"50\" value=\""
				. esc_html( $query->recurrence_expansion_start ) . "\" />\n";

			$info->rows[ 'Recurrence exp. end date' ] = "<input type=\"text\" name=\"query_recurrence_expansion_end\" size=\"50\" value=\""
				. esc_html( $query->recurrence_expansion_end ) . "\" />\n";

			$info->rows[ 'Minimum start date type' ] =
				$this->get_radio_button_input( 'query_start_min_relative', array( 'relative' => 'Relative', 'absolute' => 'Absolute', 'default' => 'Default' ),
					$query->start_min_relative, "Note: If Relative, use the offset. If Absolute, use the start date. Default is 1970-01-01.\n" );

			$info->rows[ 'Minimum start date offset' ] = "<input type=\"text\" name=\"query_start_min_offset\" size=\"50\" value=\""
				. esc_html( $query->start_min_offset ) . "\" /><br />\n" .
				"Number of days relative to the current date. Negative values are before current date, positive values are after current date.\n";

			$info->rows[ 'Minimum start date' ] = "<input type=\"text\" name=\"query_start_min\" size=\"50\" value=\""
				. esc_html( $query->start_min ) . "\" />\n";

			$info->rows[ 'Maximum start date type' ] =
				$this->get_radio_button_input( 'query_start_max_relative', array( 'relative' => 'Relative', 'absolute' => 'Absolute', 'default' => 'Default' ),
					$query->start_max_relative, "Note: If Relative, use the offset. If Absolute, use the start date. Default is 2031-01-01.\n" );

			$info->rows[ 'Maximum start date offset' ] = "<input type=\"text\" name=\"query_start_max_offset\" size=\"50\" value=\""
				. esc_html( $query->start_max_offset ) . "\" /><br />\n" .
				"Number of days relative to the current date. Negative values are before current date, positive values are after current date.\n";

			$info->rows[ 'Maximum start date' ] = "<input type=\"text\" name=\"query_start_max\" size=\"50\" value=\""
				. esc_html( $query->start_max ) . "\" />\n";
		}

		$this->render_form_table( $info );
	}

	public function build_query_description( $query = null )
	{
		$details = array();
		$details[] = "Only show all future events? " . ( $query->futureevents ? 'Yes' : 'No' );
		$details[] = "Expand recurring events into single events? " . ( $query->singleevents ? 'Yes' : 'No' );

		if ( ! $query->futureevents )
		{
			$details[] = 'Minimum start date: ' . $this->build_boundary_date( $query->start_min_relative, $query->start_min_offset, $query->start_min );
			$details[] = 'Maximum start date: ' . $this->build_boundary_date( $query->start_max_relative, $query->start_max_offset, $query->start_max );
		}

		return implode( "<br />\n", $details ) . "\n";
	}

	private function build_boundary_date( $type, $offset, $date )
	{
		$result = '';

		switch ( $type )
		{
			case 'relative':
				
				if ( $offset == 0 )
				{
					$result = 'Current date';
				}

				else if ( $offset < 0 )
				{
					$result = ( 0 - $offset ) . ' days before current date';
				}

				else
				{
					$result = $offset . ' days after current date';
				}

				break;

			case 'absolute':
				$result = $date;
				break;

			case 'default':
				$result = 'Default';
				break;

			default:
				throw new Exception( "Unrecognized boundary date type: " . $type );
		}

		return $result;
	}

}

class Flexical_Admin_Query_View extends Flexical_Admin_Basic_Query_View
{
	public function get_selected_tab()
	{
		return $this->QUERIES_TAB;
	}

	public function get_messages( $response )
	{
		$messages = array();

		if ( $response instanceof Flexical_Admin_Show_Queries_Response && $response->canceled )
		{
			$messages[] = new Flexical_Admin_Info_Message( "Action canceled." );
		}

		if ( $response instanceof Flexical_Admin_Delete_All_Queries_Response )
		{
			if ( $response->confirmed )
			{
				$messages[] = new Flexical_Admin_Message( Flexical_Admin_Message::$INFO, "All queries were deleted." );
			}

			else
			{
				$messages[] = new Flexical_Admin_Message( Flexical_Admin_Message::$ERROR, "The confirmation box for the Delete All Queries button was not selected." );
			}
		}

		if ( $response instanceof Flexical_Admin_Create_Query_Response )
		{
			$messages[] = new Flexical_Admin_Message( Flexical_Admin_Message::$INFO, "Created query " . $response->query->id . ": " . $response->query->title );
		}

		if ( $response instanceof Flexical_Admin_Update_Query_Response )
		{
			$messages[] = new Flexical_Admin_Message( Flexical_Admin_Message::$INFO, "Updated query " . $response->query->id . ": " . $response->query->title );
		}

		if ( $response instanceof Flexical_Admin_Delete_Query_Response )
		{
			$messages[] = new Flexical_Admin_Message( Flexical_Admin_Message::$INFO, "Deleted query " . $response->query->id . ": " . $response->query->title );
		}

		if ( $response instanceof Flexical_Admin_Cant_Delete_Queries_In_Use_Response )
		{
			$messages[] = new Flexical_Admin_Error_Message( "You must remove all of the listings before you can remove all of the queries." );
		}

		if ( $response instanceof Flexical_Admin_Cant_Delete_Query_In_Use_Response )
		{
			$text = '<p>Query ' . $response->query->id . ' "' . $response->query->title . '" is used in one or more listings. ';
			$text .= "You must remove this query from the listings or remove the listings before the query can be removed.</p>\n";
			$text .= "<p>Listings that use this query:</p>\n";
			$text .= $this->build_listings_in_use_table( $response->listings_using_query );

			$messages[] = new Flexical_Admin_Error_Message( $text );
		}

		return $messages;
	}

	public function render_details( $response )
	{
		$this->render_queries_table( $response->queries );
		echo( "<p>&nbsp;</p>\n" );
		$this->render_create_query_form();
		echo( "<p>&nbsp;</p>\n" );

		if ( sizeof( $response->queries ) > 0 )
		{
			$this->render_delete_all_queries_form();
			echo( "<p>&nbsp;</p>\n" );
		}
	}

	private function render_queries_table( $queries = null )
	{
		echo( "<h3>Queries</h3>\n" );

		$headers = array( 'ID', 'Query Title', 'Description', 'Actions' );

		echo( "<table class=\"widefat\">\n" );
		echo( "<thead><tr><th>" . implode( "</th><th>", $headers ) . "</th></tr></thead>\n" );

		foreach ( $queries as $id => $query )
		{
			$row = array(
				$query->id,
				esc_html( $query->title ),
				$this->build_query_description( $query ),
				$this->build_query_actions( $query )
			);

			echo( "<tr><td>" . implode( "</td><td>", $row ) . "</td></tr>\n" );
		}

		echo( "<tfoot><tr><th>" . implode( "</th><th>", $headers ) . "</th></tr></tfoot>\n" );
		echo( "</table>\n" );
	}

	private function build_query_actions( $query = null )
	{
		return  "<a href=\"" . $this->get_request_url( 'edit_query', array( 'query_id' => $query->id ) ) . "\">Edit</a> | " .
			"<a href=\"" . $this->get_request_url( 'confirm_delete_query', array( 'query_id' => $query->id ) ) . "\">Delete</a>\n";
	}

	private function render_create_query_form()
	{
		echo( "<h3>Create a New Query</h3>\n" );
		echo( "<p>Complete the form below to create a new query.</p>" );
		$this->render_query_form( null, 'create_query', 'Create Query', false, 'readwrite' );
	}

	private function render_delete_all_queries_form()
	{
		echo( "<h3>Delete All Queries</h3>\n" );
		echo( "</p>This action will delete all of the queries. <b>WARNING: This action cannot be undone.</b></p>" );

		$info = new Flexical_Admin_Form_Table_Info();
		$info->request_type = 'delete_all_queries';
		$info->submit_button_text = "Delete All Queries";
		$info->cancel_enabled = false;
		$info->rows[ 'Confirm' ] = $this->get_confirmation_checkbox();

		$this->render_form_table( $info );
	}
}

class Flexical_Admin_Confirm_Delete_Query_View extends Flexical_Admin_Basic_Query_View
{
	public function get_selected_tab()
	{
		return $this->QUERIES_TAB;
	}

	public function render_details( $response )
	{
		echo( "<h3>Are you sure you want to delete this query?</h3>\n" );
		$this->render_query_form( $response->query, 'delete_query', 'Delete Query', true, 'readonly' );
	}
}

class Flexical_Admin_Edit_Query_View extends Flexical_Admin_Basic_Query_View
{
	public function get_selected_tab()
	{
		return $this->QUERIES_TAB;
	}

	public function render_details( $response )
	{
		echo( "<h3>Edit Query</h3>\n" );
		$this->render_query_form( $response->query, 'update_query', 'Update Query', true, 'readwrite' );
	}
}
?>
